<?php
/**
 * 工作日志管理(查看、查询、设置)
 * Enter description here ...
 * @author cc
 *
 */
namespace Home\Controller;
use Think\Controller;
class WorkLogSysController extends CommonController{
public function daily(){
		$model = M();
		
		$user_name_session = session("user_name");
		$weekly = M("worklog_daily_set");
		$data = $weekly->where("header = '$user_name_session'")->find();
		if(  empty( $data ) ){
			$this->error("对不起，你没有权限访问！");
		}else{
	
			if( !isset($_POST['clickopt']) ){
				if( !isset( $_REQUEST['p'] ) )
					$_SESSION['daily'] = array();
				$_SESSION['daily']['month'] = date('Y-m');
			}else{
				if( $_POST['clickopt'] == 'search' ){
					$_SESSION['daily']['user_name']  = $_POST['user_name'];
					$_SESSION['daily']['month']      = $_POST['month'];
					$_SESSION['daily']['p']          = $_REQUEST['p'];
				}
			}
			$user_name = $_SESSION['daily']['user_name'];
			$month = $_SESSION['daily']['month'];			
			if( !empty($user_name) ){
				$where = "d.user_name = '$user_name' and ds.user_name=d.user_name and ";
			}else{
				$where = "ds.header='$user_name_session' and ds.user_name=d.user_name and ";
			}
			if( !empty($month) ){
				$where .= "date_format(d.log_date,'%Y-%m')='$month' and ";
			}
			$sql = "select d.*,u.name from Oa_worklog_daily_set ds, Oa_worklog_daily d, oa_sys_user u where ".$where." d.user_name=u.user_name and d.status=1 order by d.log_stime desc";
			$list = $model->query($sql);
			$count = count($list);
			$page = new \Think\Page($count, C("PAGE_LIST_ROWS"));
			$show = $page->show();
			
			$sql .= " limit ".$page->firstRow.','.$page->listRows;
			$list = $model->query($sql);
			
			$sql1 = "select ds.user_name, u.name from Oa_worklog_daily_set ds, oa_sys_user u where ds.header='$user_name_session' and ds.user_name = u.user_name";
			$user_arr = $model->query($sql1);
		}
		$this->assign("user_arr", $user_arr);
		$this->assign("data", $list);
		$this->assign("empty",emptyhtml("list"));
		$this->assign("user_name", $user_name);
		$this->assign("month", $month);
		$this->assign("page", $show);
		$this->display();
//		}
	}
	
	public function daily_look(){
		$id = I('get.id');
		$model = M();
		$user_name = session("user_name");	
		$sql = "select * from oa_worklog_daily d, oa_worklog_daily_set ds where d.log_id=$id and d.user_name=ds.user_name and ds.header='$user_name' limit 0,1";
		$data = $model->query($sql);
		$this->assign("vo", $data[0]);
		$this->display();
	}
	
	public function daily_search(){
		if( !isset($_POST['clickopt']) ){
			if( !isset( $_REQUEST['p'] ) ){
				$_SESSION['daily_search'] = array();
				$_SESSION['daily_search']['month'] = date('Y-m');
			}
		}else{
			if( $_POST['clickopt'] == 'search' ){
				$_SESSION['daily_search']['user_name']  = $_POST['user_name'];
				$_SESSION['daily_search']['month']      = $_POST['month'];
				$_SESSION['daily_search']['p']          = $_REQUEST['p'];
			}
		}
		$user_name = $_SESSION['daily_search']['user_name'];
		$month = $_SESSION['daily_search']['month'];
		$where = "ds.user_name = u.user_name";
		if( !empty($user_name) ){
			$where .= " and ds.user_name = '$user_name'";
		}

		$sql = "select ds.user_name, u.name as username, d.dept_name from oa_worklog_daily_set ds, oa_sys_user u, oa_sys_dept d where ";
		$sql .= $where." and u.dept_id=d.id order by d.id";
			
		$model = M();
		$data = $model->query($sql);
		$count = count($data);
		$page = new \Think\Page($count, C("PAGE_LIST_ROWS"));
		$show = $page->show();
		$sql .= " limit $page->firstRow , $page->listRows";
		
		$data = $model->query($sql);
		foreach ($data as $key=>$value){
			$user_name_val = $value['user_name'];
			$data[$key]['count'] = M("Oa_worklog_daily")->where("user_name='$user_name_val' and date_format(log_date,'%Y-%m')='$month' and status=1")->count();
			$data[$key]['log_date'] = M("Oa_worklog_daily")->where("user_name='$user_name_val' and date_format(log_date,'%Y-%m')='$month' and status=1")->order('log_stime desc')->getField('log_date');
		}
		
		$sql1 = "select ds.user_name, u.name from Oa_worklog_daily_set ds, oa_sys_user u where ds.user_name = u.user_name";
		$user_arr = $model->query($sql1);
		
		$this->assign("user_arr", $user_arr);
		$this->assign('user_name', $user_name);
		$this->assign('month', $month);
		$this->assign('data', $data);
		$this->assign("empty",emptyhtml("list"));
		$this->assign("page",$show);
		$this->display();
	}
	
	public function daily_search_view(){
		$user_name = $_REQUEST['user_name'];
		$month = $_SESSION['daily_search']['month'];
		$model = M();
		$sql = "SELECT d.*, u.name FROM oa_sys_user u, oa_worklog_daily d WHERE d.user_name='$user_name' AND u.user_name=d.user_name and date_format(d.log_date,'%Y-%m')='$month' and d.status=1 order by d.log_date";
		$data = $model->query($sql);
		$this->assign('data', $data);
		$this->assign("empty",emptyhtml("list"));
		$this->display();
	}
	
	public function weekly(){
		$model = M();
		
		$user_name_session = session("user_name");
		$weekly = M("worklog_weekly_set");
		$data = $weekly->where("header = '$user_name_session'")->find();
		if( empty( $data ) ){
			$this->error("对不起，你无法查看到下面人员的周报信息！");
		}else{
			if( !isset($_POST['clickopt']) ){
				if( !isset( $_REQUEST['p'] ) )
					$_SESSION['weekly'] = array();
				$_SESSION['weekly']['month'] = date('Y-m');
			}else{
				if( $_POST['clickopt'] == 'search' ){
					$_SESSION['weekly']['user_name']  = $_POST['user_name'];
					$_SESSION['weekly']['month']      = $_POST['month'];
					$_SESSION['weekly']['p']          = $_REQUEST['p'];
				}
			}
			$user_name = $_SESSION['weekly']['user_name'];
			$month = $_SESSION['weekly']['month'];			
			if( !empty($user_name) ){
				$where = "w.user_name = '$user_name' and ws.user_name=w.user_name and ";
			}else{
				$where = "ws.header='$user_name_session' and ws.user_name=w.user_name and ";
			}
			if( !empty($month) ){
				$where .= "date_format(w.log_date,'%Y-%m')='$month' and ";
			}
			$sql = "select w.*,u.name from Oa_worklog_weekly_set ws, Oa_worklog_weekly w, oa_sys_user u where ".$where." w.user_name=u.user_name and w.status=1 order by w.log_stime desc";
			$list = $model->query($sql);
			$count = count($list);
			$page = new \Think\Page($count, C("PAGE_LIST_ROWS"));
			$show = $page->show();
			
			$sql .= " limit ".$page->firstRow.','.$page->listRows;
			$list = $model->query($sql);
			
			$sql1 = "select ws.user_name, u.name from Oa_worklog_weekly_set ws, oa_sys_user u where ws.header='$user_name_session' and ws.user_name = u.user_name";
			$user_arr = $model->query($sql1);
		}
		$this->assign("user_arr", $user_arr);
		$this->assign("data", $list);
		$this->assign("empty",emptyhtml("list"));
		$this->assign("user_name", $user_name);
		$this->assign("month", $month);
		$this->assign("page", $show);
		$this->display();
	}
	
	public function weekly_search(){
		if( !isset($_POST['clickopt']) ){
			if( !isset( $_REQUEST['p'] ) ){
				$_SESSION['weekly_search'] = array();
				$_SESSION['weekly_search']['month'] = date('Y-m');
			}
		}else{
			if( $_POST['clickopt'] == 'search' ){
				$_SESSION['weekly_search']['user_name']  = $_POST['user_name'];
				$_SESSION['weekly_search']['month']      = $_POST['month'];
				$_SESSION['weekly_search']['p']          = $_REQUEST['p'];
			}
		}
		$user_name = $_SESSION['weekly_search']['user_name'];
		$month = $_SESSION['weekly_search']['month'];
		$where = "ws.user_name = u.user_name";
		if( !empty($user_name) ){
			$where .= " and ws.user_name = '$user_name'";
		}

		$sql = "select ws.user_name, u.name as username, d.dept_name from oa_worklog_weekly_set ws, oa_sys_user u, oa_sys_dept d where ";
		$sql .= $where." and u.dept_id=d.id order by d.id";
			
		$model = M();
		$data = $model->query($sql);
		$count = count($data);
		$page = new \Think\Page($count, C("PAGE_LIST_ROWS"));
		$show = $page->show();
		$sql .= " limit $page->firstRow , $page->listRows";
		
		$data = $model->query($sql);
		foreach ($data as $key=>$value){
			$user_name_val = $value['user_name'];
			$data[$key]['count'] = M("worklog_weekly")->where("user_name='$user_name_val' and date_format(log_date,'%Y-%m')='$month' and status=1")->count();
			$data[$key]['log_date'] = M("worklog_weekly")->where("user_name='$user_name_val' and date_format(log_date,'%Y-%m')='$month' and status=1")->order('log_stime desc')->getField('log_date');
		}
		
		$sql1 = "select ws.user_name, u.name from Oa_worklog_weekly_set ws, oa_sys_user u where ws.user_name = u.user_name";
		$user_arr = $model->query($sql1);
		
		$this->assign("user_arr", $user_arr);
		$this->assign('user_name', $user_name);
		$this->assign('month', $month);
		$this->assign('data', $data);
		$this->assign("empty",emptyhtml("list"));
		$this->assign("page",$show);
		$this->display();
	}
	
	public function weekly_search_view(){
		$user_name = $_REQUEST['user_name'];
		$month = $_SESSION['weekly_search']['month'];
		$model = M();
		$sql = "SELECT w.*, u.name FROM oa_sys_user u, oa_worklog_weekly w WHERE w.user_name='$user_name' and u.user_name=w.user_name and date_format(w.log_date,'%Y-%m')='$month' and w.status=1 order by w.log_date";
		$data = $model->query($sql);
		$this->assign('data', $data);
		$this->assign("empty",emptyhtml("list"));
		$this->display();
	}
	
	public function weekly_look(){
		$id = I('get.id');
		$model = M();
		$user_name = session("user_name");	
		$sql = "select * from oa_worklog_weekly w, oa_worklog_weekly_set ws where w.log_id=$id and w.user_name=ws.user_name and ws.header='$user_name' limit 0,1";
		$data = $model->query($sql);
		$this->assign("vo", $data[0]);
		$this->display();
	}
	
	public function monthly(){
		$model = M();
		$user_name_session = session("user_name");
		$weekly = M("worklog_monthly_set");
		$data = $weekly->where("header = '$user_name_session'")->find();
		if( empty( $data ) ){
			$this->error("对不起，你无法查看到下面人员的周报信息！");
		}else{
			if( !isset($_POST['clickopt']) ){
				if( !isset( $_REQUEST['p'] ) )
					$_SESSION['monthly'] = array();
				$_SESSION['monthly']['month'] = date("Y-m", strtotime("+1 month"));
			}else{
				if( $_POST['clickopt'] == 'search' ){
					$_SESSION['monthly']['user_name']  = $_POST['user_name'];
					$_SESSION['monthly']['month']      = $_POST['month'];
					$_SESSION['monthly']['p']          = $_REQUEST['p'];
				}
			}
			$user_name = $_SESSION['monthly']['user_name'];
			$month = $_SESSION['monthly']['month'];			
			if( !empty($user_name) ){
				$where = "w.user_name = '$user_name' and ws.user_name=w.user_name and ";
			}else{
				$where = "ws.header='$user_name_session' and ws.user_name=w.user_name and ";
			}
			if( !empty($month) ){
				$where .= "date_format(w.log_date,'%Y-%m')='$month' and ";
			}
			
			$sql = "select w.*,u.name from Oa_worklog_monthly_set ws, Oa_worklog_monthly w, oa_sys_user u where ".$where."w.user_name=u.user_name and w.status=1 order by w.log_stime desc";
			$list = $model->query($sql);
			$count = count($list);
			$page = new \Think\Page($count, C("PAGE_LIST_ROWS"));
			$show = $page->show();
			
			$sql .= " limit ".$page->firstRow.','.$page->listRows;
			$list = $model->query($sql);
			
			$sql1 = "select ws.user_name, u.name from Oa_worklog_monthly_set ws, oa_sys_user u where ws.header='$user_name_session' and ws.user_name = u.user_name";
			$user_arr = $model->query($sql1);
		}
		$this->assign("user_arr", $user_arr);
		$this->assign("data", $list);
		$this->assign("user_name", $user_name);
		$this->assign("month", $month);
		$this->assign("page", $show);
		$this->display();
	}
	
	public function monthly_search(){
		load('extend');
		if( !isset($_POST['clickopt']) ){//连接进入列表
			if( !isset( $_REQUEST['p'] ) ){
				$_SESSION['monthly_search'] = array();
				$_SESSION['monthly_search']['month'] = date("Y-m", strtotime("+1 month"));
			}
		}else{//搜索查询
			if( $_POST['clickopt'] == 'search' ){
				$_SESSION['monthly_search']['user_name']  = $_POST['user_name'];
				$_SESSION['monthly_search']['month']      = $_POST['month'];
				$_SESSION['monthly_search']['p']          = $_REQUEST['p'];
			}
		}
		$user_name = $_SESSION['monthly_search']['user_name'];
		$month = $_SESSION['monthly_search']['month'];
		$where = "ms.user_name = u.user_name and ";
		if( !empty($user_name) ){
			$where .= "ms.user_name = '$user_name' and ";
		}
		
		$sql = "select ms.user_name, u.name as username, d.dept_name from oa_worklog_monthly_set ms, oa_sys_user u, oa_sys_dept d where ";
		$sql .= $where." u.dept_id=d.id order by d.id";
			
		$model = M();
		$data = $model->query($sql);
		$count = count($data);
		$page = new \Think\Page($count, C("PAGE_LIST_ROWS"));
		$show = $page->show();
		$sql .= " limit $page->firstRow , $page->listRows";
		
		$data = $model->query($sql);
		foreach ($data as $key=>$value){
			$user_name_val = $value['user_name'];
			$data_m = M("worklog_monthly")->where("user_name='$user_name_val' and date_format(log_date,'%Y-%m')='$month' and status=1")->find();
			$data[$key]['log_name'] = $data_m['log_name'];
			$data[$key]['log_stime'] = $data_m['log_stime'];
			$data[$key]['log_date'] = $data_m['log_date'];
		}
		
//		dump($data);
		$sql1 = "select ms.user_name, u.name from oa_worklog_monthly_set ms, oa_sys_user u where ms.user_name = u.user_name";
		$user_arr = $model->query($sql1);
		
		$this->assign("user_arr", $user_arr);
		$this->assign('user_name', $user_name);
		$this->assign('month', $month);
		$this->assign('data', $data);
		$this->assign("page",$show);
		$this->display();
	}
	
	public function monthly_look(){
		$id = I('get.id');
		$model = M();
		$user_name = session("user_name");	
		$sql = "select * from oa_worklog_monthly m, oa_worklog_monthly_set ms where m.log_id=$id and m.user_name=ms.user_name and ms.header='$user_name' limit 0,1";
		$data = $model->query($sql);
		if(count($data) == 0){
			$count = 0;
		}else{
			$log_centent = explode("%!%", $data[0]['log_content']);
			foreach ($log_centent as $key=>$value){
				$log_list = explode("%%", $value);
				$vo[$key]['log_text'] = $log_list[0];
				$vo[$key]['weight'] = $log_list[1];
				$vo[$key]['over_time'] = $log_list[2];
			}
			$count = count($data[0]);
		}
		
		$this->assign("list", $vo);
		$this->display();
	}
	
	public function debriefings(){
		$user_name_session = session("user_name");
		if($user_name_session != 'chenyun'){
			$this->error("对不起，你无权限查看月度述职报告！");
		}else{

			if( !isset($_POST['clickopt']) ){
				if( !isset( $_REQUEST['p'] ) )
					$_SESSION['debriefings'] = array();
				$_SESSION['debriefings']['month'] = date("Y-m");
			}else{
				if( $_POST['clickopt'] == 'search' ){
					$_SESSION['debriefings']['user_name']  = $_POST['user_name'];
					$_SESSION['debriefings']['month']      = $_POST['month'];
					$_SESSION['debriefings']['p']          = $_REQUEST['p'];
				}
			}
			$user_name = $_SESSION['debriefings']['user_name'];
			$month = $_SESSION['debriefings']['month'];
		
			$ds = M("worklog_debriefings_set");
			$model = M();
			$ds_user_name = $ds->where("id=1")->getField("user_name");
			if( empty($ds_user_name) ){
				$list = array();
				$user_arr = array();
			}else{
				$where = "(";
				$where1 = "";
				$user_arr = explode(",", $ds_user_name);
				foreach ($user_arr as $value){
					$where .= "d.user_name='$value' or ";
					$where1 .= "user_name='$value' or ";
				}
				$where = substr($where, 0, -3);
				$where1 = substr($where1, 0, -3);
				$where .= ") and ";
				
				if(!empty($user_name)){
					$where = "u.user_name='$user_name' and ";
				}
				if(!empty($month)){
					$where .= "date_format(d.send_date,'%Y-%m')='$month' and ";
				}
				
				$sql = "select d.*, u.name as username from oa_worklog_debriefings d, oa_sys_user u where ".$where."d.user_name = u.user_name and d.status=1 order by d.send_date desc";
				$data = $model->query($sql);
				
				$count = count($data);
				$page = new \Think\Page($count, C("PAGE_LIST_ROWS"));
				$show = $page->show();
				
				$sql .= " limit ".$page->firstRow.','.$page->listRows;
				$list = $model->query($sql);
				
				$sql1 = "select user_name, name from oa_sys_user where ".$where1;//查询需要提交月度述职报告的人员
				$user_arr = $model->query($sql1);
			}
			$this->assign("user_arr", $user_arr);
			$this->assign("data", $list);
			$this->assign("user_name", $user_name);
			$this->assign("month", $month);
			$this->assign("page", $show);
			$this->assign("empty",emptyhtml("list"));
			$this->display();
		}
	}
	
	public function debriefings_search(){
		load('extend');
		if( !isset($_POST['clickopt']) ){//连接进入列表
			if( !isset( $_REQUEST['p'] ) ){
				$_SESSION['debriefings_search'] = array();
				$_SESSION['debriefings_search']['month'] = date("Y-m");
			}
		}else{//搜索查询
			if( $_POST['clickopt'] == 'search' ){
				$_SESSION['debriefings_search']['user_name']  = $_POST['user_name'];
				$_SESSION['debriefings_search']['month']      = $_POST['month'];
				$_SESSION['debriefings_search']['p']          = $_REQUEST['p'];
			}
		}
		$user_name = $_SESSION['debriefings_search']['user_name'];
		$month = $_SESSION['debriefings_search']['month'];
		$where = "";
		
		
		$sql = "select wd.*, u.user_name, u.name as username from oa_worklog_debriefings wd, oa_sys_user u where ";
		
		$model = M();
		$data_user = M("worklog_debriefings_set")->where("id=1")->getField('user_name');
		$data_user_arr = explode(",", $data_user);
		$user_arr = array();
		$sql1 = "select user_name, name from oa_sys_user where ";
		$where1 = "";
		if( !empty($data_user) ){
			$where1 = "(";
			foreach ($data_user_arr as $value){
				$where1 .= " user_name='$value' or";
			}
			$where1 = substr($where1, 0, -3);
			$where1 .= ")";
			$sql1 .= $where1;
		}
		$user_arr = $model->query($sql1);
		if( !empty($user_name) ){
			$where = "wd.user_name = '$user_name' and ";
		}else{
			$where = "(";
			foreach ($data_user_arr as $value){
				$where .= " wd.user_name='$value' or";
			}
			$where = substr($where, 0, -3);
			$where .= ") and ";
		}
		if( !empty($month) ){
			$where .= "date_format(wd.send_date,'%Y-%m')='$month' and ";
		}
		$sql .= $where."wd.user_name = u.user_name and wd.status=1";
		
		
		$data = $model->query($sql);
		$count = count($data);
		$page = new \Think\Page($count,20);
		$show = $page->show();
		$sql .= " limit $page->firstRow , $page->listRows";
		
		$data = $model->query($sql);
		
		$this->assign("user_arr", $user_arr);
		$this->assign('user_name', $user_name);
		$this->assign('month', $month);
		$this->assign('data', $data);
		$this->assign("page",$show);
		$this->assign("empty",emptyhtml("list"));
		$this->display();
	}
	
	public function daily_set(){
//		$model = new Model();
//		$sql = "SELECT u.user_name, u.name, u.job, d.dept_name FROM oa_sys_user u, oa_sys_dept d WHERE u.status='在职' AND u.user_name != 'admin' and u.dept_id=d.id order by d.id asc";
//		$data = $model->query($sql);
//		$data_user = M("oa_worklog_daily_set")->where("id=1")->find();
//		$user_arr = array();
//		if(empty($data_user)){
//			$this->assign("id", '');
//		}else{
//			if(!empty($data_user['user_name'])){
//				$user_arr = explode(",", $data_user['user_name']);
//			}
//			$this->assign("id", $data_user['id']);
//		}
//		$this->assign("user_arr", $user_arr);
//		$this->assign("data", $data);
		$this->display();
	}
	
	public function daily_set_update(){
//		$user_name = $_POST['user_name'];
//		$id = $_POST['id'];
//		$model = M("oa_worklog_daily_set");
//		if( empty($user_name) ){
//			$data['user_name'] = '';
//		}else{
//			$data['user_name'] = implode(",", $user_name);
//		}
//		if($id != '1'){
//			$data['id'] = 1;
//			$success = $model->add($data);
//		}else{
//			$success = $model->where("id=1")->save($data);
//		}
//		
//		$this->success("保存成功","daily_set");
	}
	
	public function weekly_set(){
		
		
		$this->display();
	}
	
	public function weekly_set_update(){
		
		
		
		$this->display();
	}
	
	public function monthly_set(){
		
		
		$this->display();
	}
	
	public function monthly_set_update(){
		
		
		
		$this->display();
	}
	public function debriefings_set(){
		$model = M();
		$sql = "SELECT u.user_name, u.name, u.job, d.dept_name FROM oa_sys_user u, oa_sys_dept d WHERE u.status='在职' AND u.user_name != 'admin' and u.dept_id=d.id order by d.id asc";
		$data = $model->query($sql);
		$data_user = M("worklog_debriefings_set")->where("id=1")->find();
		$user_arr = array();
		if(empty($data_user)){
			$this->assign("id", '');
		}else{
			if(!empty($data_user['user_name'])){
				$user_arr = explode(",", $data_user['user_name']);
			}
			$this->assign("id", $data_user['id']);
		}
		$this->assign("user_arr", $user_arr);
		$this->assign("data", $data);
		$this->display();
	}
	
	public function debriefings_set_update(){
		$user_name = $_POST['user_name'];
		$id = $_POST['id'];
		$model = M("worklog_debriefings_set");
		if( empty($user_name) ){
			$data['user_name'] = '';
		}else{
			$data['user_name'] = implode(",", $user_name);
		}
		if($id != '1'){
			$data['id'] = 1;
			$success = $model->add($data);
		}else{
			$success = $model->where("id=1")->save($data);
		}
		
		$this->success("保存成功","debriefings_set");
	}
    
    public function _empty($name){
    	$this->emptyaction($name);
    }
}
?>